﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using CoreShop.Model;
using CoreShop.Repository;
using SqlSugar;
namespace CoreShop.Services
{
    /// <summary>
    /// 接口实现
    /// </summary>
    public class corecmsuserServices : Repository<corecmsuser>, IcorecmsuserServices
    {
        public bool InsertAsync(corecmsuser entity)
        {
            return base.Change<corecmsuser>().Insert(entity);
        }
        public bool InsertOrUpdate(List<corecmsuser> entity)
        {
            return base.InsertOrUpdate(entity);
        }
        /// <summary>
        /// 重写异步更新方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(corecmsuser entity)
        {
            return base.Change<corecmsuser>().Update(a => new corecmsuser()
            {
                userName = entity.userName,
                creatorId = entity.creatorId,
                sysUserId = entity.sysUserId,
                passWord = entity.passWord,
                mobile = entity.mobile,
                sex = entity.sex,
                birthday = entity.birthday,
                avatarImage = entity.avatarImage,
                nickName = entity.nickName,
                balance = entity.balance,
                giftBalance = entity.giftBalance,
                point = entity.point,
                grade = entity.grade,
                createTime = entity.createTime,
                updataTime = entity.updataTime,
                status = entity.status,
                parentId = entity.parentId,
                userWx = entity.userWx,
                isDelete = entity.isDelete,
                quantity = entity.quantity,
                storeId = entity.storeId,
                corpId = entity.corpId,
                userCategoryId = entity.userCategoryId,
                source = entity.source,
                shareFlag = entity.shareFlag,
                modifyTime = entity.modifyTime,
                starDate = entity.starDate,
                endDate = entity.endDate,
                number = entity.number,
            }, a => a.id == entity.id);
        }

        /// <summary>
        /// 重写异步更新方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(List<corecmsuser> entity)
        {
            bool returnKey = true;
            foreach (var item in entity)
            {
                returnKey = base.Change<corecmsuser>().Update(a => new corecmsuser()
                {
                    userName = item.userName,
                    creatorId = item.creatorId,
                    sysUserId = item.sysUserId,
                    passWord = item.passWord,
                    mobile = item.mobile,
                    sex = item.sex,
                    birthday = item.birthday,
                    avatarImage = item.avatarImage,
                    nickName = item.nickName,
                    balance = item.balance,
                    giftBalance = item.giftBalance,
                    point = item.point,
                    grade = item.grade,
                    createTime = item.createTime,
                    updataTime = item.updataTime,
                    status = item.status,
                    parentId = item.parentId,
                    userWx = item.userWx,
                    isDelete = item.isDelete,
                    quantity = item.quantity,
                    storeId = item.storeId,
                    corpId = item.corpId,
                    userCategoryId = item.userCategoryId,
                    source = item.source,
                    shareFlag = item.shareFlag,
                    modifyTime = item.modifyTime,
                    starDate = item.starDate,
                    endDate = item.endDate,
                    number = item.number,
                }, a => a.id == item.id);
            }
            return returnKey;
        }

        /// <summary>
        /// 重写删除指定ID的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteByIdAsync(long id)
        {
            return base.Change<corecmsuser>().Update(a => new corecmsuser()
            {
                isDelete = true
            }, a => a.id == id);
        }

        /// <summary>
        /// 重写删除指定ID集合的数据(批量删除)
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DeleteByIdsAsync(int[] ids)
        {
            bool returnKey = true;
            foreach (int id in ids)
            {
                returnKey = base.Change<corecmsuser>().Update(a => new corecmsuser()
                {
                    isDelete = true
                }, a => a.id == id);
            }
            return returnKey;
        }


        #region 获取缓存的所有数据==========================================================
        /// <summary>
        /// 获取缓存的所有数据
        /// </summary>
        /// <returns></returns>
        public List<corecmsuser> GetCaChe()
        {
            return base.GetList();
        }
        #endregion


        #region 根据条件查询分页数据
        /// <summary>
        /// 根据条件查询分页数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsuser> QueryPageAsync(ref int count, int? pageIndex, int? pageSize, Int64 typeId, string sName)
        {
            List<corecmsuser> custos = new List<corecmsuser>();
            var where = Expressionable.Create<corecmsuser>();
            where = where.And(a => a.isDelete == false);
            if (typeId > 0)
            {
                where = where.And(a => a.userCategoryId == typeId);
            }
            if (!string.IsNullOrEmpty(sName))
            {
                where = where.And(a => a.userName.Contains(sName) || a.number.Contains(sName)
                    || a.mobile.Contains(sName) || a.nickName.Contains(sName));
            }
            if (pageIndex >= 1 && pageSize > 0)
            {
                custos = base.AsQueryable().WhereIF(true, where.ToExpression())
                    .LeftJoin<corecmsusergrade>((f, s) => f.grade == s.id)
                    .Select((f, s) => new corecmsuser
                    {
                        id = f.id,
                        number = f.number,
                        userName = f.userName,
                        creatorId = f.creatorId,
                        sysUserId = f.sysUserId,
                        passWord = f.passWord,
                        mobile = f.mobile,
                        sex = f.sex,
                        birthday = f.birthday,
                        avatarImage = f.avatarImage,
                        nickName = f.nickName,
                        balance = f.balance,
                        giftBalance = f.giftBalance,
                        point = f.point,
                        grade = f.grade,
                        createTime = f.createTime,
                        updataTime = f.updataTime,
                        status = f.status,
                        parentId = f.parentId,
                        userWx = f.userWx,
                        isDelete = f.isDelete,
                        quantity = f.quantity,
                        storeId = f.storeId,
                        corpId = f.corpId,
                        userCategoryId = f.userCategoryId,
                        source = f.source,
                        shareFlag = f.shareFlag,
                        modifyTime = f.modifyTime,
                        gradeName = s.title,
                        starDate = f.starDate,
                        endDate = f.endDate
                    }).ToPageList((int)pageIndex, (int)pageSize, ref count).ToList();
            }
            else
            {
                custos = base.AsQueryable().WhereIF(true, where.ToExpression()).OrderBy(a => a.id).ToList();
            }
            return custos;
        }
        #endregion

        public List<corecmsuser> SelectByCorpId(int corpId, int storeId)
        {
            return base.GetList(a => a.corpId == corpId && a.storeId == storeId && a.isDelete == false).OrderBy(a => a.id).ToList();
        }

        #region
        /// <summary>
        /// 根据条件查询数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsuser> QueryAsync(long typeId)
        {
            List<corecmsuser> list = new List<corecmsuser>();
            var where = Expressionable.Create<corecmsuser>();
            where = where.And(a => a.isDelete == false && a.id > 0);
            if (typeId > 0)
            {
                where = where.And(a => a.number.Contains(Convert.ToString(typeId))).Or(a => a.mobile.Contains(Convert.ToString(typeId)));
            }
            list = base.AsQueryable().WhereIF(true, where.ToExpression()).OrderBy(a => a.id).ToList();
            return list;
        }
        /// <summary>
        /// 根据条件查询数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public corecmsuser SelectUserById(long id)
        {
            var model = base.Change<corecmsuser>().GetSingle(a => a.id == id);
            if (model != null)
            {
                var userGrade = new corecmsusergradeServices().GetById(model.grade);
                if (userGrade != null)
                {
                    model.discountType = userGrade.discountType;
                    model.discountValue = userGrade.discountValue;
                }
            }
            return model;
        }
        #endregion
    }
}